﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DBAccessor.Accessor;
using AlanUtilities;
using DBList.Entities;
using DBList;
using System.Collections.Generic;

public partial class AddMember : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		String strUID = OperateControl.PickupAvaiableUserID(this);
		if (!IsPostBack)
		{
			int		nUID;
			int.TryParse(strUID, out nUID);
			string strOpeUserID = Request.QueryString.Get("ArticleId");
			if(strOpeUserID==null){
				Session[@"LogUserId"] = null;
				OperateControl.PickupAvaiableUserID(this);
				return;
			}
            MemberAccessor memberSystem = new MemberAccessor();
			UserData usrData = memberSystem.GetMemberInfo(nUID);
			if (usrData.GroupId != 1 && usrData.GroupId != 2){
				Session[@"LogUserId"] = null;
				OperateControl.PickupAvaiableUserID(this);
				return;
			}
			int.TryParse(strOpeUserID, out nUID);
			operaterLabel.Text = strOpeUserID;

			BindDrpCategory();
			if (nUID > 0) usrData = memberSystem.GetMemberInfo(nUID);
			else usrData = null;
			if (nUID > 0 && usrData != null && usrData.MemberId > 0)
			{
				drpUsing.Items.Add(new ListItem(@"正常", "0"));
				drpUsing.Items.Add(new ListItem(@"关闭", "1"));
				for (int i = 0; i < drpClass.Items.Count; i++)
				{
					int	nGroupID = 0;
					int.TryParse(drpClass.Items[i].Value, out nGroupID);
					if(nGroupID == usrData.GroupId){
						drpClass.SelectedIndex = i;
						break;
					}
				}
				tbAccount.Text = usrData.MemberName;
				tbPWD1.Text = "";
				tbPWD2.Text = "";
				tbRealName.Text = usrData.RealName;
				tbMDName.Text = usrData.Introduction;
				tbAddress.Text = usrData.Address;
				tbMDPhone.Text = usrData.Email;
				tbUserPhone.Text = usrData.Phone;
				if (usrData.AvailableState > 10)
					drpUsing.SelectedIndex = 1;
				else drpUsing.SelectedIndex = 0;
			}
			else {
				drpUsing.Enabled = false;
			}
			EnDisable();
		}
	}
	private void EnDisable()
	{
		ListItem selItem = drpClass.SelectedItem;
		bool	bEnable = (selItem.Value == @"5");
		tbMDPhone.Enabled = bEnable;
		tbAddress.Enabled = bEnable;
		tbMDName.Enabled = bEnable;
	}
	protected void BindDrpCategory()
	{
		CPNAccessor accessor = new CPNAccessor();
		List<RoleData> list = accessor.GetRoleData();
		foreach (RoleData clsData in list)
		{
			drpClass.Items.Add(new ListItem(clsData.RoleName, clsData.RoleID.ToString()));
		}
	}
	protected void drpClass_SelectedIndexChanged(object sender, EventArgs e)
	{
		EnDisable();
		tbMDName.Text = "";
		tbAddress.Text = "";
		tbMDPhone.Text = "";
	}
	protected void btnSubmit_Click(object sender, EventArgs e)
	{
		string	strOpeUserID = operaterLabel.Text;
		int nOpeUserID = 0;
		int.TryParse(strOpeUserID, out nOpeUserID);

		string pwd1 = tbPWD1.Text.Trim();
		string pwd2 = tbPWD2.Text.Trim();
		if(pwd1 != pwd2){
			Label2.Text = @"两次密码不同！";
			tbPWD1.Attributes.Add("value", pwd1);
			tbPWD2.Attributes.Add("value", pwd2);
			return;
		}
		else if((pwd1.Length > 0 && pwd1.Length < 6) || (nOpeUserID == 0 && pwd1.Length == 0))
		{
			Label2.Text = @"密码必需6位以上！";
			tbPWD1.Attributes.Add("value", pwd1);
			tbPWD2.Attributes.Add("value", pwd2);
			return;
		}
		string strAccount = tbAccount.Text.Trim();
		string strRealName = tbRealName.Text.Trim();
		if(strAccount.Length < 3){
			Label2.Text = @"帐号必需多于3位！";
			tbPWD1.Attributes.Add("value", pwd1);
			tbPWD2.Attributes.Add("value", pwd2);
			return;
		}
		if(strRealName.Length < 2){
			Label2.Text = @"姓名必需规范！";
			tbPWD1.Attributes.Add("value", pwd1);
			tbPWD2.Attributes.Add("value", pwd2);
			return;
		}
		string strAddress = tbAddress.Text.Trim();
		string strMDName = tbMDName.Text.Trim();
		ListItem selItem = drpClass.SelectedItem;
		if(selItem.Value == @"5"){
			if(strAddress.Length < 5 || strMDName.Length < 5){
				tbPWD1.Attributes.Add("value", pwd1);
				tbPWD2.Attributes.Add("value", pwd2);
				Label2.Text = @"门店的地址和名称必需规范！";
				return;
			}
		}
		UserData	usrData = new UserData();
		usrData.GroupId = int.Parse(selItem.Value);
		usrData.MemberName = strAccount;
		if (pwd1.Length > 0)
			usrData.MemberPass = OperateControl.GetMD5String(pwd1);
		usrData.RealName = strRealName;
		usrData.Phone = tbUserPhone.Text.Trim();
		usrData.Introduction = strMDName;
		usrData.Address = strAddress;
		usrData.Email = tbMDPhone.Text.Trim();
		if(nOpeUserID > 0){
			if (drpUsing.SelectedIndex == 1)
				usrData.AvailableState = 11;
			else usrData.AvailableState = 0;
			usrData.MemberId = nOpeUserID;
		}
		int nRet = -10;
		MemberAccessor	memAcce = new MemberAccessor();
		if (nOpeUserID == 0)
			nRet = memAcce.AddMember(usrData);
		else {
			nRet = memAcce.UpdateMember(usrData);
		}
		if(nRet == -100){
			tbPWD1.Attributes.Add("value", pwd1);
			tbPWD2.Attributes.Add("value", pwd2);
			Label2.Text = @"用户帐号已存在，请另取！";
			return;
		}
		else if(nRet == 0){
			Response.Redirect("MemberManage.aspx");
		}
		else {
			Label2.Text = @"异常！请重试！";
			return;
		}
	}
}
